/**
 * fastclick模块,参考app.ft.com.注意：此模块与jqm里面的虚拟事件冲突
 * @author levinhuang
 * @requires jf.core
 */
JF.M("fastclick",(function($){
	var p={},
		pub={};

	p.support={
		userAgentStr:window.navigator.userAgent.toLowerCase()
	};
	p.support.isAndroid=(p.support.userAgentStr.search('android') > -1);
	p.support.isiOS=(p.support.userAgentStr.search('iphone') > -1 ||
			p.support.userAgentStr.search('ipad') > -1 || 
			p.support.userAgentStr.search('ipod') > -1 || 
			p.support.userAgentStr.search('playbook') > -1);
	p.support.positionFixedSupport=(p.support.isAndroid && ('onhashchange' in window));
	p.support.cssTransitions= ("WebKitTransitionEvent" in window);

	p.support.touchable=(function(){
		var touchSupport=true;
		if (! ('createTouch' in document)){
			try {
				var touchevent = document.createEvent('TouchEvent');
				if (!touchevent.initTouchEvent) {
					touchSupport = false;
				};
			} catch(ex) {
				touchSupport = false
			};//try
		};//if
		return touchSupport;
	})();

	p.fastclick = {
		trackingClick:true,
		clickStart:{
			x:0,
			y:0,
			scroll:0
		},
		elmFromPoint:function(){
			var t = document.elementFromPoint(this.clickStart.x, this.clickStart.y + ((p.support.isAndroid && !p.support.positionFixedSupport) ? window.pageYOffset: 0));
			return t;
		},
		onTouchStart:function(e){
			var base=p.fastclick;
			base.trackingClick = true;
			base.clickStart.x = e.targetTouches[0].clientX;
			base.clickStart.y = e.targetTouches[0].clientY;
			base.clickStart.scroll = window.pageYOffset;
			return true;
		},
		onTouchMove:function(e){
			var base=p.fastclick;
			if (base.trackingClick) {
				if (
					Math.abs(e.targetTouches[0].clientX - base.clickStart.x) > 10 || 
					Math.abs(e.targetTouches[0].clientY - base.clickStart.y) > 10 ) {
					base.trackingClick = false;
				};
			};
			return true;
		},
		onTouchEnd:function(e){
			var base=p.fastclick;
			if (!base.trackingClick || 
				//_columnsAnimating || 
				Math.abs(window.pageYOffset - base.clickStart.scroll) > 5) { 
				return true;
			};
			var t = base.elmFromPoint();
			if (t.nodeType == Node.TEXT_NODE) {
				t = t.parentNode;
			};
			$(t).trigger('vclick');

			var clickEvent = document.createEvent('MouseEvents');
			clickEvent.initEvent('click', true, true);
			clickEvent.forwardedTouchEvent = true;
			t.dispatchEvent(clickEvent);
			if (t.className.indexOf('needsclick') != -1) return false;
			if (t.nodeName != 'SELECT') e.preventDefault();

		},
		onTouchCancel:function(e){
			p.fastclick.trackingClick = false;
		},
		onClick:function(e){
			if (!window.event) return true;

			var allowClick = true,
				base=p.fastclick,
				t = base.elmFromPoint();

			if (t && t.className.indexOf('needsclick') != -1) return true;

			if (!window.event.forwardedTouchEvent && p.support.touchable) {
				allowClick = false
			};
			if (allowClick) {
				return true;
			};
			e.stopPropagation();
			e.preventDefault();
			return false;
		},
		init: function() {
			if (p.support.touchable) {
				document.body.addEventListener('touchstart', this.onTouchStart, true);
				document.body.addEventListener('touchmove', this.onTouchMove, true);
				document.body.addEventListener('touchend', this.onTouchEnd, true);
				document.body.addEventListener('touchcancel', this.onTouchCancel, true)
			};
			document.body.addEventListener('click', this.onClick, true);

		}
	};//fastclick

	pub.onLoad=function(){
		p.fastclick.init();
	};

	return pub;

})(jQuery));